# Contributor: Carlo Landmeter <clandmeter@gmail.com>
# Maintainer: Timo Teräs <timo.teras@iki.fi>
pkgname=grub
pkgver=2.04
pkgrel=2
pkgdesc="Bootloader with support for Linux, Multiboot and more"
url="https://www.gnu.org/software/grub/"
arch="all !s390x"
license="GPL-3.0-or-later"
depends="mkinitfs"
triggers="grub.trigger=/boot"
makedepends="bison flex linux-headers xz-dev lvm2-dev fuse-dev
	automake autoconf libtool python3 freetype-dev unifont gettext-dev"
install="$pkgname.post-upgrade"
# strip handled by grub Makefiles, abuild strip breaks xen pv-grub
options="!check !strip"  # Cannot test boot loader.
subpackages="
	$pkgname-dev
	$pkgname-doc
	$pkgname-mkfont
	$pkgname-mount
	$pkgname-bash-completion:bashcomp:noarch
	"

# currently grub only builds on x86*, aarch64 and ppc* systems
case "$CARCH" in
	x86)		flavors="efi bios";;
	x86_64)		flavors="efi bios xenhost xenhost_pvh";;
	aarch64|arm*)	flavors="efi";;
	mips*)		flavors="qemu_mips";;
	ppc*)		flavors="ieee1275"; makedepends="$makedepends powerpc-utils" ;;
	s390x)		flavors="emu" ;;
esac
for f in $flavors; do
	[ x"$f" = x"xenhost_pvh" ] && continue # xenhost_pvh shipped in the xenhost subpackage
	subpackages="$subpackages $pkgname-$f"
done

source="https://ftp.gnu.org/gnu/grub/grub-$pkgver.tar.xz
	fix-gcc-no-pie-specs.patch
	grub-xen-host_grub.cfg
	alpine-mkconfig.patch
	lang-C.UTF-8.patch
	"

prepare() {
	default_prepare
	PYTHON=python3 ./autogen.sh
}

_build_flavor() {
	local flavor="$1"
	shift
	local _configure="$@"

	msg "Building grub for platform $flavor"
	mkdir -p "$srcdir"/build-$flavor
	cd "$srcdir"/build-$flavor
	$builddir/configure \
		--build=$CBUILD \
		--host=$CHOST \
		--target=$CTARGET \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--localstatedir=/var \
		--disable-nls \
		--disable-werror \
		$_configure
	make
}

build() {
	local f
	for f in $flavors; do
		case "$f" in
		bios)		_build_flavor $f --with-platform=pc;;
		efi)		_build_flavor $f --with-platform=efi --disable-efiemu;;
		xenhost)	_build_flavor $f --with-platform=xen;;
		xenhost_pvh)	_build_flavor $f --with-platform=xen_pvh --target=i386;;
		*)		_build_flavor $f --with-platform=$f;;

		esac
	done
}

_install_xen() {
	grub_dir=$(mktemp -d)
	cfg=$(mktemp)
	grub_memdisk=$(mktemp)

	mkdir -p $grub_dir/boot/grub
	echo 'normal (memdisk)/grub.cfg' > $cfg
	sed -e "s/@@PVBOOT_ARCH@@/$CARCH/g" \
		$srcdir/grub-xen-host_grub.cfg \
			> $grub_dir/grub.cfg
	tar -cf - -C $grub_dir grub.cfg > $grub_memdisk

	./grub-mkimage \
		-O $CARCH-xen \
		-c $cfg \
		-d ./grub-core ./grub-core/*.mod \
		-m $grub_memdisk \
		-o $pkgdir/grub-$CARCH-xen.bin

	rm -r "$grub_dir"
	rm "$cfg" "$grub_memdisk"
}

_install_xenpvh() {
	grub_dir=$(mktemp -d)
	cfg=$(mktemp)
	grub_memdisk=$(mktemp)

	mkdir -p $grub_dir/boot/grub
	echo 'normal (memdisk)/grub.cfg' > $cfg
	sed -e "s/@@PVBOOT_ARCH@@/$CARCH/g" \
		$srcdir/grub-xen-host_grub.cfg \
			> $grub_dir/grub.cfg
	tar -cf - -C $grub_dir grub.cfg > $grub_memdisk

	echo "About to run grub-mkimage in: $PWD"
	# Note: only i386-xen_pvh supported
	./grub-mkimage \
		-O i386-xen_pvh \
		-c $cfg \
		-d ./grub-core ./grub-core/*.mod \
		-m $grub_memdisk \
		-o $pkgdir/grub-i386-xen_pvh.bin

	rm -r "$grub_dir"
	rm "$cfg" "$grub_memdisk"
}

_install_flavor() {
	local flavor="$1"
	cd "$srcdir"/build-$flavor
	case $flavor in
	xenhost)	_install_xen;;
	xenhost_pvh)	_install_xenpvh;;
	*)	 	make DESTDIR="$pkgdir" install-strip;;
	esac
}

package() {
	# install BIOS & EFI version into the same directory
	# and overwrite similar files.
	for f in $flavors; do
		_install_flavor $f
	done

	rm -f "$pkgdir"/usr/lib/charset.alias
	# remove grub-install warning of missing directory
	mkdir -p "$pkgdir"/usr/share/locale

	mkdir -p "$pkgdir"/etc/default/
	cat >"$pkgdir"/etc/default/grub <<-EOF
	GRUB_DISTRIBUTOR="Alpine"
	GRUB_TIMEOUT=2
	GRUB_DISABLE_SUBMENU=y
	GRUB_DISABLE_RECOVERY=true
	EOF

	mkdir -p "$pkgdir"/usr/share/bash-completion
	mv "$pkgdir"/etc/bash_completion.d "$pkgdir"/usr/share/bash-completion/completions
}

bios() {
	pkgdesc="$pkgdesc (BIOS version)"
	depends="$pkgname"
	mkdir -p $subpkgdir/usr/lib/grub
	mv $pkgdir/usr/lib/grub/*-pc $subpkgdir/usr/lib/grub/
}

efi() {
	pkgdesc="$pkgdesc (EFI version)"
	depends="$pkgname"
	mkdir -p $subpkgdir/usr/lib/grub
	mv $pkgdir/usr/lib/grub/*-efi $subpkgdir/usr/lib/grub/
}

xenhost() {
	pkgdesc="$pkgdesc (XEN host version)"
	mkdir -p $subpkgdir/usr/lib/grub-xen
	mv $pkgdir/*-xen.bin $subpkgdir/usr/lib/grub-xen/

	# pick up result from xenhost_pvh build as well
	mv $pkgdir/*-xen_pvh.bin $subpkgdir/usr/lib/grub-xen/
}

qemu_mips() {
	pkgdesc="$pkgdesc (QEMU MIPS version)"
	depends="$pkgname"
	mkdir -p $subpkgdir/usr/lib/grub
	mv $pkgdir/usr/lib/grub/*-qemu_mips $subpkgdir/usr/lib/grub/
}

ieee1275() {
	pkgdesc="$pkgdesc (IEEE1275 version)"
	depends="$pkgname powerpc-utils"
	mkdir -p $subpkgdir/usr/lib/grub
	mv $pkgdir/usr/lib/grub/*-ieee1275 $subpkgdir/usr/lib/grub/
}

emu() {
	pkgdesc="$pkgdesc (EMU version)"
	depends="$pkgname"
	mkdir -p $subpkgdir/usr/lib/grub
	mv $pkgdir/usr/lib/grub/*-emu $subpkgdir/usr/lib/grub/
}

mkfont() {
	pkgdesc="$pkgdesc (mkfont utility)"
	depends="$pkgname"
	mkdir -p $subpkgdir/usr/bin
	mv $pkgdir/usr/bin/grub-mkfont $subpkgdir/usr/bin/
}

mount() {
	pkgdesc="$pkgdesc (mount utility)"
	depends="$pkgname"
	mkdir -p $subpkgdir/usr/bin
	mv $pkgdir/usr/bin/grub-mount $subpkgdir/usr/bin/
}

bashcomp() {
	depends=""
	pkgdesc="Bash completions for $pkgname"
	install_if="$pkgname=$pkgver-r$pkgrel bash-completion"

	amove usr/share/bash-completion/completions
}

sha512sums="9c15c42d0cf5d61446b752194e3b628bb04be0fe6ea0240ab62b3d753784712744846e1f7c3651d8e0968d22012e6d713c38c44936d4004ded3ca4d4007babbb  grub-2.04.tar.xz
f2a7d9ab6c445f4e402e790db56378cecd6631b5c367451aa6ce5c01cd95b95c83c3dd24d6d4b857f8f42601eba82c855607513eb6ce5b2af6bd6c71f046e288  fix-gcc-no-pie-specs.patch
4e7394e0fff6772c89683039ccf81099ebbfe4f498e6df408977a1488fd59389b6e19afdbf0860ec271e2b2aea0df7216243dcc8235d1ca3af0e7f4d0a9d60a4  grub-xen-host_grub.cfg
84d9694af384336c0846faad968a63b154e2bd957671fa7c0163c36e20fd086a8d5b67ddf5eb983a11a69f65dac52668d0f51537e7cfe9a1ea9bca7aba91f20b  alpine-mkconfig.patch
f23702741f2a37c83d0cbf16a000a6229dfd433cd583f3254d523ce35b3668c1db2d8a704a75013e74655ec4f9da5db6b630aa92e3ca5cc47df44cc9e41d1806  lang-C.UTF-8.patch"
